// vote-list.js
const util = require('../../utils/util.js')
const appConfig = require('../../config.js')
const app = getApp()

Page({
  data: {
    isLoading: true,
    myVoteList: [],
    useWxAuth: false
  },
  // 分享配置
  onShareAppMessage: function (res) {
    var url = 'pages/index/index'
    return {
      title: '投票',
      path: url
    }
  },
  // 发起我的投票
  createVote: function () {
    wx.navigateTo({
      url: '../vote-edit/vote-edit'
    })
  },
  // 获取列表
  initVoteList: function () {
    wx.showLoading()
    app.requestWithCredentials({
      url: appConfig.APP_URL_PREFIX + 'vote_list/',
      method: "GET",
      data: {},
      success: (res) => {
        wx.hideLoading()
        if (res.data.code === 0) {
          this.data.myVoteList = res.data.data
          this.setData({
            myVoteList: res.data.data
          })
        }
        this.setData({
          isLoading: false
        })
      }
    })
  },
  goToVote: function (event) {
    var voteData = event.currentTarget.dataset.vote
    var voteId = voteData.id
    wx.navigateTo({
      url: '../vote-info/vote-info?voteId=' + voteId
    })
  },
  goToIndex: function(){
    wx.navigateTo({
      url: '../index/index'
    })
  },
  onLoad: function () {
    wx.hideLoading();
    if (!app.globalData.userInfo) {
      this.setData({
        useWxAuth: true
      })
    } else {
      this.initVoteList();
    }
  }
})
